#include "stdafx.h"
#include "DataServer.h"
#include "DataPool.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
DataServer DataServer::_server;

DataServer* DataServer::getInstance()
{
	return &_server;
}


DataServer::DataServer()
{
    DataPool* numericPool  = new NumericPool();
    DataPool* gridPool     = new GridPool();
    DataPool* pairPool     = new PairPool();
    DataPool* lakePool     = new LakePool();
    DataPool* riverPool    = new RiverPool();
    DataPool* riverfirstPool  = new RiverFirstPool();
    DataPool* riversecondPool = new RiverSecondPool();
    DataPool* riverthirdPool  = new RiverThirdPool();
    DataPool* lakefirstPool   = new LakeFirstPool();
    DataPool* lakesecondPool  = new LakeSecondPool();
    DataPool* lakethirdPool   = new LakeThirdPool();
    DataPool* sortPool = new SortPool();

    addPool(numericPool);
    addPool(gridPool);
    addPool(pairPool);
    addPool(lakePool);
    addPool(riverPool);
    addPool(riverfirstPool);
    addPool(riversecondPool);
    addPool(riverthirdPool);
    addPool(lakefirstPool);
    addPool(lakesecondPool);
    addPool(lakethirdPool);
    addPool(sortPool);
}

DataServer::~DataServer()
{

}

void DataServer::addPool(DataPool *pool)
{
	pools.push_back(pool);
}

DataPool* DataServer::getPool(DataPool::PoolName name)
{
	DataPool *pool;

	std::vector<DataPool*>::iterator iter;
	for(iter=pools.begin(); iter<pools.end(); iter++)
	{
		pool = *iter;
		if( pool->poolName == name )
        {
            return pool;
        }
	}

    return NULL;
}

void DataServer::Push(int N)
{
	DataPool *pool;

	std::vector<DataPool*>::iterator iter;
	for(iter=pools.begin(); iter<pools.end(); iter++)
	{
		pool = *iter;
		pool->Push(N);
	}
}

void DataServer::clearData()
{
	DataPool *pool;

	std::vector<DataPool*>::iterator iter;
	for(iter=pools.begin(); iter<pools.end(); iter++)
	{
		pool = *iter;
		pool->clearData();
	}
}


// API
BetPool* GetBetPool(int N)
{
   if(N<=N36)
    {
        NumericPool* numericPool = (NumericPool*)DataServer::getInstance()->getPool( DataPool::Numeric);
        return (BetPool*)numericPool->getPool(N);
    }
    else if( (N >= Grid1) && (N <= Grid36) )
    {
        GridPool* gridPool = (GridPool*)DataServer::getInstance()->getPool( DataPool::Grid);
        return (BetPool*)gridPool->getPool(N);
    }

    return NULL;
}
